// pages/chooseMaintainer/chooseMaintainer.js
const app = getApp()
const util = require('../../utils/util.js')
const lbbService = require('../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabNum:1,
    userTypes:[{name:'人员',id:1},{name:'代理商',id:2}]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      hospitalId: options.hosId?options.hosId:0,
      type:options.type?options.type:1,
      scrollHeight: app.getScrollHeight(options.type==1?100:50)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  toggleTab: function(e){
    let that = this;
    that.setData({
      tabNum:e.currentTarget.dataset.id
    })
  },
  /**
   * 添加常用
   */
  addRecent: function(){
    wx.navigateTo({
      url: '../SCM/store/addressAdd/addressAdd?type=recentUser',
    })
  },
  inputName: function(e){
    let that = this
    console.log(e)
    that.setData({
      inputName: e.detail.value
    })
  },
  searchName: function (e) {
    let that = this
    let searchName = that.data.inputName;
    let agents = [];
    let installs = [];
    let agentSolid = that.data.agentSolid || [];
    let installSolid = that.data.installSolid || [];
    if (searchName) {
      agentSolid.forEach(item=>{
        agents.push({
          name:item.name,
          maintains:item.maintains.filter(maintainerItem=>maintainerItem.maintainName.indexOf(searchName)!=-1)
        })
      })
      installSolid.forEach(item=>{
        installs.push({
          name:item.name,
          maintains:item.maintains.filter(maintainerItem=>maintainerItem.maintainName.indexOf(searchName)!=-1)
        })
      })
    } else {
      agents = agentSolid;
      installs = installSolid;
    }
    that.setData({
      agent: agents,
      install: installs,
    })
  },
  selectMaintainer: function (e) {
    console.log(e)
    let pages = getCurrentPages()
    let prevPage = pages[pages.length - 2]
    let id = e.currentTarget.dataset.id
    let maintainName = e.currentTarget.dataset.name
    let maintainerInfo = {
      maintainUid: id,
      maintainName: maintainName
    }
    prevPage.setData({
      maintainers: maintainerInfo
    })
    wx.navigateBack({
      delta: 1
    })
  },
  getList:function(){
    let that = this
    lbbService.getMaintenanceStaff({
      type:that.data.type,//1 表示从投后问题反馈进入 2 表示从仓管进入
      hospitalId:parseInt(that.data.hospitalId)
    },function(res){
      if (res.data) {
        that.setData({
          agent: res.data.agentMaintain.maintainInfo,
          agentSolid: res.data.agentMaintain.maintainInfo,
          install: res.data.instaillMaintain.maintainInfo,
          installSolid: res.data.instaillMaintain.maintainInfo,
        })
      }
    })
  }
})